package com.ykh.service.impl;

import com.ykh.pojo.dto.CategoryDto;
import com.ykh.pojo.po.Category;
import com.ykh.mapper.CategoryMapper;
import com.ykh.pojo.vo.Result;
import com.ykh.service.CategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ykh
 * @since 2023年10月26日
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public Category getCategoryById(Long categoryId) {
        return categoryMapper.selectById(categoryId);
    }

    /**
     * 添加分类
     * @param categoryDto
     */
    @Override
    public Result add(CategoryDto categoryDto) {
        Category category = new Category();
        category.setCategoryName( categoryDto.getCategoryName());
        category.setAvatar(categoryDto.getAvatar());
        category.setDescription(categoryDto.getDescription());
        category.setCreator("admin");
        category.setCreateDate(LocalDateTime.now());
        categoryMapper.insert(category);
        return Result.success("添加成功",null);
    }

    /**
     * 删除分类
     * @param categoryId
     * @return
     */
    @Override
    public Result delete(Integer categoryId) {
        int n = categoryMapper.deleteById(categoryId);
        if(n==0){
            return Result.success("删除的分类不存在",null);
        }
        return Result.success("删除成功",null);
    }
}
